using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Krile.Kernel.Control.Surface;
using Krile.Kernel.Control.Surface.MessageObjects;
using Krile.Kernel.WindowsEx;
using System.Management;


namespace Krile.Forms.Dialogs.Show
{
    public partial class Version : Form
    {
        public Version()
        {
            InitializeComponent();
        }

        private void Version_Load(object sender, EventArgs e)
        {
            statusCountLabel.Text = String.Format(statusCountLabel.Text, Core.StatusManager.GetStatusesCount());
            versionLabel.Text = "Version " + Define.VersionString;
            versionString.Text = String.Format(versionString.Text, Define.Version.ToString("0.000"), Define.Build.ToString());
        }

        private void detailText_LinkClicked(object sender, LinkClickedEventArgs e)
        {
            Subsystem.CommonOperation.CallBrowser(e.LinkText);
        }

        private void getDebugInfo_Click(object sender, EventArgs e)
        {
            //create debug text
            StringBuilder sb = new StringBuilder(100);
            sb.AppendLine(Define.AppInfoFullString);
            sb.AppendLine("---");
            sb.AppendLine("UA: " + Define.UserAgent);
            sb.AppendLine("OS: " + Environment.OSVersion.VersionString);
            sb.AppendLine("---");
            using (var manage = new ManagementClass("Win32_OperatingSystem"))
            {
                foreach (var ins in manage.GetInstances())
                {
                    try
                    {
                        sb.AppendFormat("Physical memory: {0}KB/{1}KB" + Environment.NewLine,ins["FreePhysicalMemory"], ins["TotalVisibleMemorySize"]);

                        sb.AppendFormat("Virtual memory: {0}KB/{1}KB" + Environment.NewLine, ins["FreeVirtualMemory"], ins["TotalVirtualMemorySize"]);
                    }
                    finally
                    {
                        ins.Dispose();
                    }
                }
            }
            sb.AppendLine("GC allocated memory: " + GC.GetTotalMemory(true));
            sb.AppendLine("---");
            sb.AppendLine("Registered status: " + Core.StatusManager.GetStatusesCount());
            sb.AppendLine("Cached image: " + Core.ImageManager.GetCachedImagesCount());
            sb.AppendLine("---");
            sb.AppendLine("Plugin informations:");
            foreach (var plugin in Core.PluginManager.GetAllPluginDatas())
            {
                sb.AppendLine(plugin.Name + ":");
                sb.AppendLine("    Ver : " + plugin.Version);
                sb.AppendLine("    Load: " + plugin.Loadable.ToString());
                sb.AppendLine("    Desc: " + plugin.Description);
                sb.AppendLine("    Cls : " + plugin.ClassName);
                sb.AppendLine("    Path: " + plugin.FilePath);
                sb.AppendLine("    Uri : " + plugin.SupportUri);
            }
            debugText.Text = sb.ToString();
        }

    }
}